#ifndef XLCOMM_NET_EVENTLOOPTHREAD_H_
#define XLCOMM_NET_EVENTLOOPTHREAD_H_

#include <condition_variable>
#include <functional>
#include <mutex>
#include <string>
#include <thread>

#include "xlcomm/define.h"
#include "xlcomm/net/event_loop.h"
#include "xlcomm/noncopyable.h"

namespace xlcomm {
namespace net {

// IO事件循环线程
class XLCOMM_API EventLoopThread : noncopyable {
 public:
  using ThreadInitCallback = std::function<void(EventLoop*)>;

  explicit EventLoopThread(const std::string& name = std::string(),
                           const ThreadInitCallback& cb = ThreadInitCallback());
  ~EventLoopThread();

  // 等创建完成，并获取EventLoop指针
  EventLoop* StartLoop();

 private:
  // 线程处理函数
  void ThreadProc();

  std::mutex mutex_;
  std::condition_variable cond_;
  ThreadInitCallback init_callback_;
  EventLoop* event_loop_;
  bool started_;
  std::thread thread_;
  std::string name_;
};

}  // namespace net
}  // namespace xlcomm

#endif  // XLCOMM_NET_EVENTLOOPTHREAD_H_